package com.acwing.partition5;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/4/8 10:09
 */
public class AC479加分二叉树 {

    private static final int N = 35;
    private static int[] w = new int[N];
    private static int[][] f = new int[N][N], path = new int[N][N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) w[i] = Integer.parseInt(ss[i - 1]);
        for (int len = 1; len <= n; len++) {
            for (int l = 1; l + len - 1 <= n; l++) {
                int r = l + len - 1;
                if (len == 1) {
                    f[l][r] = w[l];
                    path[l][r] = l;
                    continue;
                }
                for (int k = l; k <= r; k++) {
                    int left = k == l ? 1 : f[l][k - 1], right = k == r ? 1 : f[k + 1][r];
                    int score = left * right + w[k];
                    if (f[l][r] < score) {
                        f[l][r] = score;
                        path[l][r] = k;
                    }
                }
            }
        }
        writer.write(f[1][n] + "\n");
        dfs(1, n);
        writer.flush();
    }

    private static void dfs(int l, int r) throws IOException {
        if (l > r) return;
        writer.write(path[l][r] + " ");
        dfs(l, path[l][r] - 1);
        dfs(path[l][r] + 1, r);
    }
}
